package com.xhsj.user.hr.system.service;

import com.xhsj.user.hr.process.processDefinition.query.MapQuery;
import com.xhsj.user.hr.system.entity.TbGlobalMenu;
import com.xhsj.user.hr.system.entity.TbGlobalOrganization;
import com.xhsj.user.hr.system.mapper.TbGlobalOrganizationMapper;
import com.xhsj.user.hr.system.query.GetListByParentIdQuery;
import com.xhsj.user.hr.system.service.TbGlobalOrganizationService;
import com.xhsj.user.hr.base.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.system.vo.OrganizationTreeVo;
import com.xhsj.user.utils.TreeUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 组织结构表 服务实现类
 * </p>
 *
 * @author 苏小林
 * @since Sep 1, 2020 1:50:18 PM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbGlobalOrganization")
public class TbGlobalOrganizationService extends BaseService<TbGlobalOrganizationMapper, TbGlobalOrganization>{
    @Autowired
    private TbGlobalOrganizationMapper dao;

	/**
	 * 保存
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
    @Override
    //@CacheEvict(cacheNames="TbGlobalOrganization", allEntries=true)
    public boolean save(TbGlobalOrganization entity) {
//        Integer parentId = entity.getParentId();
//        if (parentId == 1) {
//            entity.setParentIds("1");
//        } else {
//            String parentIds = dao.getParentIdsById(parentId);
//            entity.setParentIds(parentIds+","+parentId);
//        }
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbGlobalOrganization getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
    @Override
    //@CacheEvict(cacheNames="TbGlobalOrganization", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbGlobalOrganization> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbGlobalOrganization entity = JSON.parseObject(JSON.toJSONString(data), TbGlobalOrganization.class);
        Page<TbGlobalOrganization> page = new Page(currentPage,pageSize);
        QueryWrapper<TbGlobalOrganization> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author 苏小林
	 * @date Sep 1, 2020 1:50:18 PM
	 */
    @Override
    //@Cacheable(value = "TbGlobalOrganization",sync = true)
    public List<TbGlobalOrganization> selectAll() {
        return super.selectAll();
    }


    /**
     * @Description:  部门列表
     * @Param:  com.xhsj.user.hr.process.dto.ProcessDefinition.AddBusinessDto
     * @Author: liuqinghua
     * @Date: 2020/8/31
     */
    public List<MapQuery> organizationList() {
        return dao.organizationList();
    }

    /**
     * 查询组织结构部门树
     * @author 苏小林
     * @date 2020/9/2 14:41
     * @return: java.util.List<com.xhsj.user.hr.system.vo.OrganizationTreeVo>
     */
    public List<OrganizationTreeVo> findOrganizationTree() {

        List<OrganizationTreeVo> list = dao.findOrganizationTree();
        List<OrganizationTreeVo> father = list.stream().filter(organizationTreeVo -> organizationTreeVo.getId() == 100).collect(Collectors.toList());
        TreeUtils.setSubChild(father,list);
        return list;
    }

    /**
     * 查询parentId下所有列表数据
     * @author 苏小林
     * @date 2020/9/2 15:39
     * @param: getListByParentIdQuery
     * @return: java.util.List<com.xhsj.user.hr.system.entity.TbGlobalOrganization>
     */
    public IPage<TbGlobalOrganization> getListByParentId(GetListByParentIdQuery query) {
        Page<TbGlobalMenu> page = new Page(query.getPage(),query.getSize());
        return dao.getListByParentId(page,query.getParentId());
    }

    /**
     * 查询所有职位列表
     * @author 苏小林
     * @date 2020/9/2 16:53
     * @return: java.lang.Object
     */
    public List<MapQuery> findPositionList() {
        return dao.findPositionList();
    }

    /**
     * 物理删除数据
     * @author 苏小林
     * @date 2020/9/2 18:03
     * @param: id
     */
    public void delteById(Long id) {
        dao.delteById(id);
    }
}
